package com.senven.api.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senven.api.commom.ResponsePage;
import com.senven.api.dao.OrganMapper;
import com.senven.api.pojo.Organ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author LQP
 *
 */
@Service
public class OrganService {
    @Autowired
    private OrganMapper organMapper;

    public ResponsePage<Organ> queryOrganList(int current, int size){
        Page<Organ> page = new Page<Organ>(current, size);
        QueryWrapper<Organ> wrapper = new QueryWrapper<Organ>();

        IPage<Organ> iPage = organMapper.selectPage(page, null);

        ResponsePage<Organ> responsePage= new ResponsePage<Organ>();
        responsePage.makePage(iPage);

        return responsePage;
    }

    /**
     *根据ID查询社团
     * @param id
     * @return
     */
    public Organ queryOrganListById(int id){
        return organMapper.selectById(id);
    }

    /**
     * 根据名字查询社团
     * 要求前缀正确
     * @param name
     * @return
     */
    public ResponsePage<Organ> queryOrganListByName(String name,int current, int size){
        Page<Organ> page = new Page<Organ>(current, size);
        QueryWrapper<Organ> wrapper = new QueryWrapper<Organ>();
        wrapper.like("name",name);

        IPage<Organ> iPage = organMapper.selectPage(page, wrapper);

        ResponsePage<Organ> responsePage= new ResponsePage<Organ>();
        responsePage.makePage(iPage);

        return responsePage;
    }

    /**
     * 根据名字精确查询社团
     * 要求前缀正确
     * @param name
     * @return
     */
    public ResponsePage<Organ> queryOrganListByNameOne(String name,int current, int size){
        Page<Organ> page = new Page<Organ>(current, size);
        QueryWrapper<Organ> wrapper = new QueryWrapper<Organ>();
        wrapper.eq("name",name);

        IPage<Organ> iPage = organMapper.selectPage(page, wrapper);

        ResponsePage<Organ> responsePage= new ResponsePage<Organ>();
        responsePage.makePage(iPage);

        return responsePage;
    }

    /**
     * 插入一个社团
     * @param organ
     * @return
     */
    public int insertOrgan(Organ organ){return organMapper.insertOrgan(organ);};

    /**
     * 更新一个社团的信息
     */
    public int updateOrgan(Organ organ){return organMapper.updateById(organ);}

    /**
     *
     * 删除一个社团的信息
     */
    public int deleteOrgan(int id){return organMapper.deleteOrgan(id);}

}
